import os
import requests
from pathlib import Path

# 定义项目根目录
ROOT_DIR = Path(__file__).parent.parent
STATIC_DIR = ROOT_DIR / 'static'
VENDOR_DIR = STATIC_DIR / 'vendor'

# 创建必要的目录
def create_directories():
    dirs = [
        VENDOR_DIR / 'bootstrap' / 'css',
        VENDOR_DIR / 'bootstrap' / 'js',
        VENDOR_DIR / 'bootstrap-icons' / 'font',
        VENDOR_DIR / 'bootstrap-icons' / 'fonts',
        VENDOR_DIR / 'jquery',
        VENDOR_DIR / 'chartjs'
    ]
    for dir_path in dirs:
        dir_path.mkdir(parents=True, exist_ok=True)

# 定义需要下载的文件
FILES = {
    'bootstrap.min.css': {
        'url': 'https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css',
        'path': VENDOR_DIR / 'bootstrap' / 'css' / 'bootstrap.min.css'
    },
    'bootstrap.min.css.map': {
        'url': 'https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css.map',
        'path': VENDOR_DIR / 'bootstrap' / 'css' / 'bootstrap.min.css.map'
    },
    'bootstrap.bundle.min.js': {
        'url': 'https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js',
        'path': VENDOR_DIR / 'bootstrap' / 'js' / 'bootstrap.bundle.min.js'
    },
    'bootstrap.bundle.min.js.map': {
        'url': 'https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js.map',
        'path': VENDOR_DIR / 'bootstrap' / 'js' / 'bootstrap.bundle.min.js.map'
    },
    'bootstrap-icons.min.css': {
        'url': 'https://cdn.bootcdn.net/ajax/libs/bootstrap-icons/1.11.3/font/bootstrap-icons.min.css',
        'path': VENDOR_DIR / 'bootstrap-icons' / 'font' / 'bootstrap-icons.min.css'
    },
    'bootstrap-icons.woff': {
        'url': 'https://cdn.bootcdn.net/ajax/libs/bootstrap-icons/1.11.3/font/fonts/bootstrap-icons.woff',
        'path': VENDOR_DIR / 'bootstrap-icons' / 'fonts' / 'bootstrap-icons.woff'
    },
    'bootstrap-icons.woff2': {
        'url': 'https://cdn.bootcdn.net/ajax/libs/bootstrap-icons/1.11.3/font/fonts/bootstrap-icons.woff2',
        'path': VENDOR_DIR / 'bootstrap-icons' / 'fonts' / 'bootstrap-icons.woff2'
    },
    'jquery.min.js': {
        'url': 'https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js',
        'path': VENDOR_DIR / 'jquery' / 'jquery.min.js'
    },
    'chart.umd.min.js': {
        'url': 'https://cdn.bootcdn.net/ajax/libs/Chart.js/4.4.1/chart.umd.min.js',
        'path': VENDOR_DIR / 'chartjs' / 'chart.umd.min.js'
    }
}

def download_file(url, path):
    print(f'Downloading {url} to {path}...')
    response = requests.get(url)
    if response.status_code == 200:
        path.parent.mkdir(parents=True, exist_ok=True)
        with open(path, 'wb') as f:
            f.write(response.content)
        print(f'Successfully downloaded {path}')
    else:
        print(f'Failed to download {url}')

def main():
    print('Setting up static files...')
    create_directories()
    
    for file_info in FILES.values():
        download_file(file_info['url'], file_info['path'])
    
    # 修改 bootstrap-icons.min.css 中的字体路径
    icons_css_path = VENDOR_DIR / 'bootstrap-icons' / 'font' / 'bootstrap-icons.min.css'
    if icons_css_path.exists():
        with open(icons_css_path, 'r', encoding='utf-8') as f:
            content = f.read()
        content = content.replace('fonts/', '../fonts/')
        with open(icons_css_path, 'w', encoding='utf-8') as f:
            f.write(content)
        print('Updated bootstrap-icons.min.css font paths')
    
    print('Static files setup complete!')

if __name__ == '__main__':
    main() 